package com._21cn.framework.utils.crypt;

import com._21cn.framework.utils.StringUtil;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class TestEncrypt {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//3DES加密演示代码
		String deskey = StringUtil.toHex( DES.generateDESKey() );
	   	String painText = "21222324252627281122334455667788";
    	System.out.println("DES-demo***************************************");
    	System.out.println( "deskey:" + deskey );
    	System.out.println("painText:"+painText);
    	String hipText = DES.encrypt(painText,deskey,DES.Algorithm.DES_ECB_PKCS5Padding);
    	System.out.println("cipherText:"+hipText);
    	String decrypText = DES.decrypt(hipText, deskey, DES.Algorithm.DES_ECB_PKCS5Padding);
    	System.out.println("decrypText:"+decrypText);
//    	System.out.println("**************cipher-test******************************");
//    	try{
//    		decrypText = TestEncrypt.desEncrypt(painText, DES.generateDESKey().toString()).toString();
//    		System.out.println("cipher-test-3des-encrp:"+decrypText);
//    	}catch(Exception ex){
//    		ex.printStackTrace();
//    	}
    	
    	
    	//base64
    	System.out.println("**************base64******************************");
//    	String base64encrypText = StringUtil.base64Encode(painText);
//    	System.out.println("base64encrypText:"+base64encrypText);
//    	String base64decrypText = StringUtil.base64Decode(base64encrypText);
//    	System.out.println("base64decrypText:"+base64decrypText);
    	System.out.println("********************************************");
    	
    	
    	//SHA1
    	System.out.println("**************SHA1******************************");
    	String sha1Str = StringUtil.sha1(painText);
    	System.out.println("sha1Str:"+sha1Str);
    	
    	//HMAC-SHA1
    	System.out.println("**************hmac_sha1Str******************************");
    	String hmac_sha1Str = StringUtil.hmacSHA1(painText,deskey);
    	System.out.println("hmac_sha1Str:"+hmac_sha1Str);
    	
    	//md5
    	System.out.println("**************md5Str******************************");
    	String md5Str = StringUtil.md5(painText, deskey);
    	System.out.println("md5Str:"+md5Str);
    	
    	
	}
	
	
	public static byte[] desEncrypt(String message, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("DESede");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(message.getBytes("UTF-8"));
	}

}
